package org.jeecg.qis.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.qis.entity.QisMatdetailValue;
import org.jeecg.qis.mapper.QisMatdetailValueMapper;
import org.jeecg.qis.service.IQisMatdetailValueService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: qis-来料检测结果明细表
 * @Author: shaolong
 * @Date: 2022-10-09
 * @Version: V1.0
 */
@Service
public class QisMatdetailValueServiceImpl extends ServiceImpl<QisMatdetailValueMapper, QisMatdetailValue> implements IQisMatdetailValueService {

	@Autowired
	private QisMatdetailValueMapper qisMatdetailValueMapper;

	@Override
	public List<QisMatdetailValue> selectByMainId(String mainId) {
		return qisMatdetailValueMapper.selectByMainId(mainId);
	}

	@Override
	public Result<String> edit(Map<String, String> param) {
		QisMatdetailValue qisMatdetailValue = new QisMatdetailValue();
		if (param.containsKey("recId")) {
			qisMatdetailValue = this.getById(param.get("recId"));
			quantitativeCalculation(qisMatdetailValue, param);
		} else {
			qisMatdetailValue = this.getById(param.get("id"));
			qualitativeCalculation(qisMatdetailValue, param);
		}
		LambdaUpdateWrapper<QisMatdetailValue> eq = new LambdaUpdateWrapper<QisMatdetailValue>();
		eq.eq(QisMatdetailValue::getRecId, qisMatdetailValue.getRecId());
		boolean update = update(qisMatdetailValue, eq);
		return update ? Result.ok("修改成功!") : Result.error("修改失败!");
	}


	private void quantitativeCalculation(QisMatdetailValue qisMatdetailValue, Map<String, String> param) {
		if (param.containsKey("lowLimit")) {
			String status = Double.parseDouble(param.get("value")) > Double.parseDouble(param.get("lowLimit")) ? "1" : "0";
			qisMatdetailValue.setStatus(status);
		}
		if (param.containsKey("upLimit")) {
			String status = Double.parseDouble(param.get("value")) < Double.parseDouble(param.get("upLimit")) ? "1" : "0";
			qisMatdetailValue.setStatus(status);
		}
		qisMatdetailValue.setValue(param.get("value"));
	}

	private void qualitativeCalculation(QisMatdetailValue qisMatdetailValue, Map<String, String> param) {
		if (param.containsKey("unqualifiedNum")) {
			qisMatdetailValue.setValue(param.get("unqualifiedNum"));
		}
		if (param.containsKey("status") && "2".equals(param.get("status"))) {
			if (Integer.parseInt(qisMatdetailValue.getValue()) > 0) {
				qisMatdetailValue.setStatus("0");
			} else {
				qisMatdetailValue.setStatus("1");
			}
		} else {
			qisMatdetailValue.setStatus(param.get("status"));
		}
	}
}
